"""
在线词典 ： 客户端 发请求 得结果 展示
"""
import sys
from socket import *


class Dict_Handle:
    def __init__(self):
        self.ADDR = ("127.0.0.1", 7779)
        self.sock = self.all()

    def all(self):
        sock = socket()
        sock.connect(self.ADDR)
        return sock

    def exit(self):
        self.sock.send(b"E")
        self.sock.close()
        sys.exit("拜拜了您嘞")

    def enrol_user(self):
        name = input("输入用户名")
        password = input("输入密码")
        num = f"R\t{name}\t{password}"
        self.sock.send(num.encode())
        data = self.sock.recv(1024).decode()
        if data == "T":
            print("注册成功")
        else:
            print("注册失败")

    def enter_user(self):
        name = input("输入用户名")
        password = input("输入密码")
        num = f"L\t{name}\t{password}"
        self.sock.send(num.encode())
        data = self.sock.recv(1024).decode()
        if data == "T":
            print("登录成功")
            return "接收"
        else:
            print("用户名或者密码错误")


class Dict_View:
    def __init__(self):
        self.handel = Dict_Handle()

    def menu_2(self):
        while True:
            print("""
        ============ 查询界面 ============
         1. 查单词   2. 历史记录   3. 注销
        =================================
            """)
            cmd = input("请输入选项:")
            if cmd == "1":
                pass
            elif cmd == "2":
                pass
            elif cmd == "3":
                break
            else:
                print("请输入正确选项！")

    # 一级界面
    def menu_1(self):
        while True:
            print("""
        ========== 登录界面 ==========
         1. 登录   2. 注册   3. 退出
        =============================
            """)
            cmd = input("请输入选项:")
            if cmd == "1":
                if self.handel.enter_user() == "接收":
                    self.menu_2()
                else:
                    continue
            elif cmd == "2":
                self.handel.enrol_user()
                self.menu_2()
            elif cmd == "3":
                self.handel.exit()
                break
            else:
                print("请输入正确选项！")


if __name__ == '__main__':
    d = Dict_View()
    d.menu_1()
